package com.itheima.day10;

import com.itheima.domain.Student;
import com.itheima.util.RandomUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/*
统计本组的平均年龄

统计各个年龄段的人数


统计各个姓的人数
 */
public class Test01 {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            Student student = new Student();
            if (i < 9) {
                student.setSid("000" + (i + 1));
            } else {
                student.setSid("00" + (i + 1));
            }

            student.setName(RandomUtil.getName());
            student.setGender(RandomUtil.getGender());
            student.setBirthday(RandomUtil.getBirthday());
            student.setPhone(RandomUtil.getPhone());
            student.setWxs(RandomUtil.getWxs());
            student.setGroup(i / 4 + 1);
            //System.out.println(student);
            list.add(student);
        }
        //list


        for (Student student : list) {
            System.out.println(student);
        }
        /*HashMap<Integer, Integer> groupAvgAge = tongjipingjunnianling(list);
        groupAvgAge.forEach((k, v) -> System.out.println(k + "组的平均年龄是" + v));*/
       /* HashMap<Integer, Integer> ageduan = tongjinianlingduanrenshu(list);
        ageduan.forEach((k, v) -> System.out.println(k + "年龄段有" + v + "个人"));*/
        HashMap<String, Integer> xingMap = Test01.tongjixing(list);
        xingMap.forEach((k, v) -> System.out.println(k + "姓有" + v + "个人"));
    }

    //统计个小组的平均年龄
    public static HashMap<Integer, Integer> tongjipingjunnianling(ArrayList<Student> list) {
        //list  所有人
        // 按组分配到一起
        //HashMap<Integer  , ArrayList<Student>>
        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            int group = student.getGroup();
            //当前组内有没有人
            if (map.containsKey(group)) {//组内已经有人了 加入即可
                map.get(group).add(student);
            } else {//还没有这个组 是第一人 创建集合
                ArrayList<Student> groupList = new ArrayList<>();
                groupList.add(student);
                map.put(group, groupList);
            }
        }
        // 遍历上面的HashMap  统计各组的平均年龄   将结果 放入新的map中  HashMap<Integer,Integer>
        HashMap<Integer, Integer> groupAvgAge = new HashMap<>();
        Set<Integer> groups = map.keySet();
        for (Integer group : groups) {
            ArrayList<Student> students = map.get(group);

            int sum = 0;
            for (int i = 0; i < students.size(); i++) {
                Student student = students.get(i);
                int age = student.getAge();
                sum += age;
            }
            int avg = sum / students.size();
            groupAvgAge.put(group, avg);

        }
        //返回Map
        return groupAvgAge;
    }

    //统计各个年龄段的人数
    public static HashMap<Integer, Integer> tongjinianlingduanrenshu(ArrayList<Student> list) {

        //按年龄段  将人放在一起 Map// 键 年龄  值
        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            int age = student.getAge();
            if (map.containsKey(age)) {
                map.get(age).add(student);
            } else {
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(age, students);
            }
        }
        // 遍历上面的集合
        HashMap<Integer, Integer> ageduan = new HashMap<>();
        Set<Integer> ages = map.keySet();
        for (Integer age : ages) {
            ArrayList<Student> students = map.get(age);
            ageduan.put(age, students.size());
        }

        return ageduan;
    }

    //统计各个姓的人数
    public static HashMap<String, Integer> tongjixing(ArrayList<Student> list) {
        HashMap<String, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            String xing = student.getName().substring(0, 1);
            if (map.containsKey(xing)) {
                map.get(xing).add(student);
            } else {
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(xing, students);
            }
        }

        HashMap<String, Integer> xingMap = new HashMap<>();
        Set<String> xings = map.keySet();
        for (String xing : xings) {
            ArrayList<Student> students = map.get(xing);
            xingMap.put(xing, students.size());
        }
        return xingMap;

    }

}
